package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.constants.CopyWriterConstant;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.CopyWriterConfigUtil;
import com.ytjj.qmyx.admin.dao.ProductImgDao;
import com.ytjj.qmyx.admin.model.request.AddProductImgRequest;
import com.ytjj.qmyx.admin.model.request.ProductImgRequest;
import com.ytjj.qmyx.admin.service.ProductImgService;
import com.ytjj.qmyx.mall.mapper.ProductImgMapper;
import com.ytjj.qmyx.mall.mapper.ProductMapper;
import com.ytjj.qmyx.mall.model.ProductImg;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ProductImgServiceImpl implements ProductImgService {

    @Resource
    private ProductImgDao productImgDao;

    @Resource
    private ProductImgMapper productImgMapper;

    @Resource
    private CopyWriterConfigUtil copyWriterConfigUtil;

    @Override
    public CommonPage<ProductImg> list(ProductImgRequest productImgRequest) {
        PageHelper.startPage(productImgRequest.getPageNum(), productImgRequest.getPageSize());
        List<ProductImg> productImgs = productImgDao.list(productImgRequest);
        return CommonPage.restPage(productImgs);
    }

    @Override
    public int create(AddProductImgRequest addProductImgRequest) {

        int count = 0;

        if (addProductImgRequest.getProductId() == null) {
            throw new ApiException(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.PRODUCT_ID_NOT_NULL));
        }

        if (!CollectionUtils.isEmpty(addProductImgRequest.getImgUrls())) {
            for (Map<String, String> param : addProductImgRequest.getImgUrls()) {
                String name = param.get("name").substring(0, param.get("name").indexOf("."));
                Integer num = 0;
                //上传图片必须为数字名称，视频不受限制
                if (addProductImgRequest.getFileType().equals("img") && !name.equals("0")) {
                    try {
                        num = Integer.parseInt(name);
                    } catch (RuntimeException e) {
                        throw new ApiException(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.FILE_NAME_MUST_NUMBER));
                    }
                } else {
                    num = 1;
                }
                ProductImg productImg = new ProductImg();
                productImg.setImgUrl(param.get("url"));
                productImg.setProductId(addProductImgRequest.getProductId());
                productImg.setSort(num);
                productImg.setType(addProductImgRequest.getType());
                productImg.setFileType(addProductImgRequest.getFileType());
                productImg.setCreateTime(new Date());
                count = productImgMapper.insert(productImg);
            }
        } else {
            throw new ApiException(copyWriterConfigUtil.getCopyWriterValue(CopyWriterConstant.FILE_NOT_NULL));
        }

        //存在推荐图需同步商品是否存在推荐图状态
        Integer isFlag = productImgDao.isRecImgFlag(addProductImgRequest.getProductId());
        if (isFlag > 0) {
            productImgDao.updateRecImgFlag(addProductImgRequest.getProductId(), 1);
        } else {
            productImgDao.updateRecImgFlag(addProductImgRequest.getProductId(), 0);
        }

        return count;
    }

    @Override
    public int update(ProductImg productImg) {
        //存在推荐图需同步商品是否存在推荐图状态
        Integer isFlag = productImgDao.isRecImgFlag(productImg.getProductId());
        if (isFlag > 0) {
            productImgDao.updateRecImgFlag(productImg.getProductId(), 1);
        } else {
            productImgDao.updateRecImgFlag(productImg.getProductId(), 0);
        }
        productImg.setUpdateTime(new Date());
        return productImgMapper.updateByPrimaryKeySelective(productImg);
    }

    @Override
    public int delete(Integer id) {
        ProductImg productImg = productImgDao.getProductImgById(id);
        //存在推荐图需同步商品是否存在推荐图状态
        Integer isFlag = productImgDao.isRecImgFlag(productImg.getProductId());
        if (isFlag > 0) {
            productImgDao.updateRecImgFlag(productImg.getProductId(), 1);
        } else {
            productImgDao.updateRecImgFlag(productImg.getProductId(), 0);
        }
        return productImgMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer delProductImg(ProductImgRequest productImgRequest) {
        Integer[] arrayId = productImgRequest.getArrayId();
        Integer count = 0;
        if(arrayId.length > 0){
            for (int i=0; i<arrayId.length; i++){
                count = productImgDao.delProductImg(arrayId[i]);
            }
        }
        return count;
    }

    @Override
    public List<ProductImg> getAllImg(Integer productId, String type) {
        return productImgDao.getAllImg(productId, type);
    }

}
